1 - Welcome friend :-)

Welcome to Sonic Pi. Hopefully you’re as excited to get started making crazy sounds as I am to show you. It’s going to be a really fun ride where you’ll learn all about music, synthesis, programming, composition, performance and more.

But wait, how rude of me! Let me introduce myself - I’m Sam Aaron - the chap that created Sonic Pi. You can find me at @samaaron on Twitter and I’d be more than happy to say hello to you. You might also be interested in finding out more about my Live Coding Performances where I code with Sonic Pi live in front of audiences.

If you have any thoughts, or ideas for improving Sonic Pi - please pass them on - feedback is so helpful. You never know, your idea might be the next big feature!

This tutorial is divided up into sections grouped by category. Whilst I’ve written it to have an easy learning progression from start to finish, feel very free just to dip in and out of sections as you see fit. If you feel that there’s something missing, do let me know and I’ll consider it for a future version.

Finally, watching others live code is a really great way to learn. I regularly stream live on https://youtube.com/samaaron so please do drop by, say hi and ask me lots of questions :-)

OK, let’s get started…


1.1 - Live Coding

One of the most exciting aspects of Sonic Pi is that it enables you to write and modify code live to make music, just like you might perform live with a guitar. This means that given some practice you can take Sonic Pi on stage and gig with it.

Free your mind

Before we get into the real details of how Sonic Pi works in the rest of this tutorial, I’d like to give you an experience of what it’s like to live code. Don’t worry if you don’t understand much (or any) of this. Just try to hold onto your seats and enjoy…

A live loop

Let’s get started, copy the following code into an empty buffer:

live_loop :flibble do
  sample :bd_haus, rate: 1
  sleep 0.5
end

Now, press the Run button and you’ll hear a nice fast bass drum beating away. If at any time you wish to stop the sound just hit the Stop button. Although don’t hit it just yet… Instead, follow these steps:

  1. Make sure the bass drum sound is still running
  2. Change the sleep value from 0.5 to something higher like 1.
  3. Press the Run button again
  4. Notice how the drum speed has changed.
  5. Finally, remember this moment, this is the first time you’ve live coded with Sonic Pi and it’s unlikely to be your last…

Ok, that was simple enough. Let’s add something else into the mix. Above sample :bd_haus add the line sample :ambi_choir, rate: 0.3. Your code should look like this:

live_loop :flibble do
  sample :ambi_choir, rate: 0.3
  sample :bd_haus, rate: 1
  sleep 1
end

Now, play around. Change the rates - what happens when you use high values, or small values or negative values? See what happens when you change the rate: value for the :ambi_choir sample just slightly (say to 0.29). What happens if you choose a really small sleep value? See if you can make it go so fast your computer will stop with an error because it can’t keep up (if that happens, just choose a bigger sleep time and hit Run again).

Try commenting one of the sample lines out by adding a # to the beginning:

live_loop :flibble do
  sample :ambi_choir, rate: 0.3
#  sample :bd_haus, rate: 1
  sleep 1
end

Notice how it tells the computer to ignore it, so we don’t hear it. This is called a comment. In Sonic Pi we can use comments to remove and add things into the mix.

Finally, let me leave you something fun to play with. Take the code below, and copy it into a spare buffer. Now, don’t try to understand it too much other than see that there are two loops - so two things going round at the same time. Now, do what you do best - experiment and play around. Here are some suggestions:

Remember to press Run and you’ll hear the change next time the loop goes round. If you end up in a pickle, don’t worry - hit Stop, delete the code in the buffer and paste a fresh copy in and you’re ready to jam again. Making mistakes is how you’ll learn the quickest…

live_loop :guit do
  with_fx :echo, mix: 0.3, phase: 0.25 do
    sample :guit_em9, rate: 0.5
  end
#  sample :guit_em9, rate: -0.5
  sleep 8
end

live_loop :boom do
  with_fx :reverb, room: 1 do
    sample :bd_boom, amp: 10, rate: 1
  end
  sleep 8
end

Now, keep playing and experimenting until your curiosity about how this all actually works kicks in and you start wondering what else you can do with this. You’re now ready to read the rest of the tutorial.

So what are you waiting for…


1.2 - The Sonic Pi Interface

Sonic Pi has a very simple interface for coding music. Let’s spend a little time exploring it.

Sonic Pi Interface

A. Play Controls

These pink buttons are the main controls for starting and stopping sounds. There’s the Run button for running the code in the editor, Stop for stopping all running code, Save for saving the code to an external file and Record to create a recording (a WAV file) of the sound playing.

B. Editor Controls

These orange buttons allow you to manipulate the code editor. The Size + and Size - buttons allow you to make the text bigger and smaller.

C. Info and Help

These blue buttons give you access to information, help and preferences. The Info button will open up the information window which contains information about Sonic Pi itself - the core team, history, contributors and community. The Help button toggles the help system (G) and the Prefs button toggles the preferences window which allows you to control some basic system parameters.

D. Code Editor

This is the area where you’ll write your code and compose/perform music. It’s a simple text editor where you can write code, delete it, cut and paste, etc. Think of it like a very basic version of Word or Google Docs. The editor will automatically colour words based on their meaning in the code. This may seem strange at first, but you’ll soon find it very useful. For example, you’ll know something is a number because it is blue.

E. Prefs Panel

Sonic Pi supports a number of tweakable preferences which can be accessed by toggling the prefs button in the Info and Help button set. This will toggle the visibility of the Prefs Panel which includes a number of options to be changed. Examples are forcing mono mode, inverting stereo, toggling log output verbosity and also a volume slider and audio selector on the Raspberry Pi.

F. Log Viewer

When you run your code, information about what the program is doing will be displayed in the log viewer. By default, you’ll see a message for every sound you create with the exact time the sound was triggered. This is very useful for debugging your code and understanding what your code is doing.

G. Help System

One of the most important parts of the Sonic Pi interface is the help system which appears at the bottom of the window. This can be toggled on and off by clicking on the blue Help button. The help system contains help and information about all aspects of Sonic Pi including this tutorial, a list of available synths, samples, examples, FX and a full list of all the functions Sonic Pi provides for coding music.

H. Scope Viewer

The scope viewer allows you to see the sound you’re hearing. You can easily see that the saw wave looks like a saw and that the basic beep is a curvey sine wave. You can also see the difference between loud and quiet sounds by the size of the lines. There are 3 scopes to play with - the default is a combined scope for the left and right channels, there is a stereo scope which draws a separate scope for each channel. Finally there is a Lissajous curve scope which will show the phase relationship between the left and right channels and allows you to draw pretty pictures with sound (https://en.wikipedia.org/wiki/Lissajous_curve).

I. Cue Viewer

All internal and external events (called cues in Sonic Pi) are automatically logged in the Cue Viewer. For example, if you have a MIDI controller connected and press one of its buttons, you’ll see a new cue event in the Cue Viewer telling you the name of the controller and which button you pressed. Once you’ve mastered the basics of making and producing sounds, you’ll start to want to cue sounds or whole sections of sounds based on events such as these. A cue event is just an indication that something happened. For example, every time a live loop spins round, it sends a cue event out which is logged in the Cue Viewer. Also, external events such as MIDI messages from connected MIDI equipment and OSC messages from other programs or computers are also displayed in the Cue Viewer. It is also possible to directly generate cue events using the cue function. Anything that appears in the Cue Viewer can be used to trigger something happening. This is covered in more detail in Sections 10 to 12 of this tutorial.


1.3 - Learning through Play

Sonic Pi encourages you to learn about both computing and music through play and experimentation. The most important thing is that you’re having fun, and before you know it you’ll have accidentally learned how to code, compose and perform.

There are no mistakes

Whilst we’re on this subject, let me just give you one piece of advice I’ve learned over my years of live coding with music - there are no mistakes, only opportunities. This is something I’ve often heard in relation to jazz but it works equally well with live coding. No matter how experienced you are - from a complete beginner to a seasoned live coder, you’ll run some code that has a completely unexpected outcome. It might sound insanely cool - in which case run with it. However, it might sound totally jarring and out of place. It doesn’t matter that it happened - what matters is what you do next with it. Take the sound, manipulate it and morph it into something awesome. The crowd will go wild.

Start Simple

When you’re learning, it’s tempting to want to do amazing things now. However, just hold that thought and see it as a distant goal to reach later. For now, instead think of the simplest thing you could write which would be fun and rewarding that’s a small step towards the amazing thing you have in your head. Once you have an idea about that simple step, then try and build it, play with it and then see what new ideas it gives you. Before long you’ll be too busy having fun and making real progress.

Just make sure to share your work with others!


2 - Synths

OK, enough of the intros - let’s get into some sound.

In this section we’ll cover the basics of triggering and manipulating synths. Synth is short for synthesiser which is a fancy word for something which creates sounds. Typically synths are quite complicated to use - especially analog synths such as Eurorack modules connected together by a mess of wires. However, Sonic Pi gives you much of that power in a very simple and approachable manner.

Don’t be fooled by the immediate simplicity of Sonic Pi’s interface. You can get very deep into very sophisticated sound manipulation if that’s your thing. Hold on to your hats…


2.1 - Your First Beeps

Take a look at the following code:

play 70

This is where it all starts. Go ahead, copy and paste it into the code window at the top of the app (the big white space under the Run button). Now, press Run…

Beep!

Intense. Press it again. And again. And again…

Woah, crazy, I’m sure you could keep doing that all day. But wait, before you lose yourself in an infinite stream of beeps, try changing the number:

play 75

Can you hear the difference? Try a lower number:

play 60

So, lower numbers make lower pitched beeps and higher numbers make higher pitched beeps. Just like on a piano, the keys at the lower part of the piano (the left hand side) play lower notes and the keys on the higher part of the piano (the right hand side) play higher notes.

It turns out that the C of the 4th octave (C in English notation) is identified by the number 60. play 60 therefore plays the C of the 4th octave. To play the next piano key to the right, you have to add 1 to 60 and then type play 61, which in this case is the black key of the C sharp. To play the D, the next key to the right, play 62.

Don’t worry if this means nothing to you - it didn’t to me when I first started. All that matters right now is that you know that low numbers make lower beeps and high numbers make higher beeps.

Chords

Playing a note is quite fun, but playing many at the same time can be even better. Try it:

play 72
play 75
play 79

Jazzy! So, when you write multiple plays, they all play at the same time. Try it for yourself - which numbers sound good together? Which sound terrible? Experiment, explore and find out for yourself.

Melody

So, playing notes and chords is fun - but how about a melody? What if you wanted to play one note after another and not at the same time? Well, that’s easy, you just need to sleep between the notes:

play 72
sleep 1
play 75
sleep 1
play 79

How lovely, a little arpeggio. So what does the 1 mean in sleep 1? Well it means the duration of the sleep. It actually means sleep for one beat, but for now we can think about it as sleeping for 1 second. So, what if we wanted to make our arpeggio a little faster? Well, we need to use shorter sleep values. What about a half i.